package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.khyr.Vo.QueryWorkloadStatBackVo;
import com.khyr.Vo.RequestQueryWorkloadVo;
import com.khyr.dao.HisRegistrationDao;
import com.khyr.entity.HisRegistration;
import com.khyr.service.WorkloadService;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：lyk
 * @date ：Created in 2025/3/14 17:02
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class WorkloadServiceImpl implements WorkloadService {
    @Autowired
    private HisRegistrationDao hisRegistrationDao;
    @Override
    public R<List<HisRegistration>> workloadController(RequestQueryWorkloadVo requestQueryWorkloadVo) {
        QueryWrapper<HisRegistration> queryWrapper = new QueryWrapper<>();
        if(requestQueryWorkloadVo.getDoctorName() != null){
            queryWrapper.like("doctor_name",requestQueryWorkloadVo.getDoctorName());
        }
        queryWrapper.eq("registration_status","3");
        if(requestQueryWorkloadVo.getDateRange() != null){
            queryWrapper.between("visit_date",requestQueryWorkloadVo.getDateRange()[0],requestQueryWorkloadVo.getDateRange()[1]);
        }else {
            queryWrapper.between("visit_date", LocalDate.now(), LocalDate.now());
        }
        List<HisRegistration> hisRegistrations = hisRegistrationDao.selectList(queryWrapper);
        return new R<>(200,"获取成功",hisRegistrations);
    }

    @Override
    public R<List<QueryWorkloadStatBackVo>> queryWorkloadStat(RequestQueryWorkloadVo requestQueryWorkloadVo) {
        QueryWrapper<HisRegistration> queryWrapper = new QueryWrapper<>();
        if(requestQueryWorkloadVo.getDoctorName() != null){
            queryWrapper.like("doctor_name",requestQueryWorkloadVo.getDoctorName());
        }
        queryWrapper.eq("registration_status","3");
        if(requestQueryWorkloadVo.getDateRange() != null){
            queryWrapper.between("visit_date",requestQueryWorkloadVo.getDateRange()[0],requestQueryWorkloadVo.getDateRange()[1]);
        }else {
            queryWrapper.between("visit_date", LocalDate.now(), LocalDate.now());
        }
        List<HisRegistration> hisRegistrations = hisRegistrationDao.selectList(queryWrapper);
        // 按医生分组统计挂号总金额和挂号数量
        Map<String, QueryWorkloadStatBackVo> doctorStats = new HashMap<>();
        for (HisRegistration registration : hisRegistrations) {
            String doctorName = registration.getDoctorName();
            String userId = String.valueOf(registration.getUserId());
            BigDecimal amount = registration.getRegistrationAmount();

            QueryWorkloadStatBackVo stat = doctorStats.computeIfAbsent(doctorName, k -> {
                QueryWorkloadStatBackVo vo = new QueryWorkloadStatBackVo();
                vo.setUserId(userId);
                vo.setDoctorName(doctorName);
                vo.setTotalPrice("0");
                vo.setCount("0");
                return vo;
            });
            // 更新总金额
            BigDecimal totalPrice = new BigDecimal(stat.getTotalPrice());
            totalPrice = totalPrice.add(amount);
            stat.setTotalPrice(totalPrice.toString());
            // 更新挂号数量
            int count = Integer.parseInt(stat.getCount());
            stat.setCount(String.valueOf(count + 1));
        }
        // 将统计结果转换为列表
        List<QueryWorkloadStatBackVo> result = new ArrayList<>(doctorStats.values());
        return new R<>(200,"获取成功",result);
    }
}
